package com.elecbook.auth.controller;

import com.elecbook.auth.model.dto.UserPQuery;
import com.elecbook.auth.model.dto.UserProfileDeleteDTO;
import com.elecbook.auth.model.dto.UserProfileSubmitDTO;
import com.elecbook.auth.model.po.User;
import com.elecbook.auth.service.UserInfoService;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description 本controller是资源controller，用于进行资源的检测
 *
 * @author zken
 * CreateDate 2024/11/2 16:11:29
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserInfoService userInfoService;


    @RequestMapping("/getUserInfo")
    public Object getUserInfo(Authentication authentication) {
        return authentication.getPrincipal();
    }

    @PreAuthorize("hasAnyRole('USER')")
    @RequestMapping("/hello")
    public Object hello() {
        return "Hello World";
    }

    @PreAuthorize("hasAnyRole('CFO')")
    @RequestMapping("/CFO")
    public Object helloCFO() {
        return "Hello CFO";
    }

    @PostMapping("/profile")
    public Result<User> getProFile() {
        User user = userInfoService.getUserInfo();
        return Result.success(user);
    }


    @PostMapping("/delete")
    public Result<String> deleteUserProfile(@RequestBody UserProfileDeleteDTO dto) {
        userInfoService.deleteUser(dto);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<User> updataUserProfile(@RequestBody UserProfileSubmitDTO dto) {
        User user = userInfoService.updataUserProfile(dto);
        return Result.success(user);
    }

    @ApiOperation("框架内交互，获取一批用户信息")
    @PostMapping("/r/userInfos")
    public Result<List<User>> getUserInfoByIds(@RequestBody List<Integer> ids) {
        List<User> list = userInfoService.getUserInfoByIds(ids);
        return Result.success(list);
    }
    
    
    
    /*
    管理员生成方法
    */

    @PostMapping("/op/save")
    Result<User> save(@RequestBody User user) {
        User res = userInfoService.saveUser(user);
        return Result.success(res);
    }

    @DeleteMapping("/op/{id}")
    Result<String> delete(@PathVariable Long id) {
        userInfoService.removeById(id);
        return Result.success();
    }

    @GetMapping("/op/{id}")
    Result<User> get(@PathVariable Long id) {
        User res =  userInfoService.getById(id);
        return Result.success(res);
    }

    @PutMapping("/op/update")
    Result<User> update(@RequestBody User user) {
        User res =  userInfoService.updateUserById(user);
        return Result.success(res);
    }

    @PostMapping("/op/page")
    Result<PageDTO<User>> page(@RequestBody UserPQuery userQuery) {
        PageDTO<User> pageResult = userInfoService.pageQuery(userQuery);
        return Result.success(pageResult);
    }
}
